import {Injectable} from '@angular/core';
import {Attendance} from '../types/resp/Attendance';
import {AttendanceForm} from '../types/req/AttendanceForm';
import {Result} from '../types/result';
import MockApi from '../api/mockApi';
import {map, Observable} from 'rxjs';
import type {Response} from '../types/response';
import {Page} from '../types/page';
import {AttendanceQueryForm} from '../types/req/AttendanceQueryForm';

@Injectable({
  providedIn: 'root'
})
export class AttendanceService {
  attendances: Attendance[] = [
    {"id":1,"employeeId":2,"employeeName":"李娜","attendanceDate":"2024-01-10","checkInTime":"08:55","checkOutTime":"18:05","status":30,"createTime":"2024-11-17 22:24:17","modifyTime":"2024-02-29 22:24:17"},
    {"id":2,"employeeId":1,"employeeName":"张伟","attendanceDate":"2024-08-04","checkInTime":"09:10","checkOutTime":"18:15","status":10,"createTime":"2024-05-12 22:24:17","modifyTime":"2024-08-02 22:24:17"},
    {"id":3,"employeeId":2,"employeeName":"李娜","attendanceDate":"2024-02-22","checkInTime":"08:50","checkOutTime":"17:55","status":40,"createTime":"2024-10-10 22:24:17","modifyTime":"2024-12-17 22:24:17"},
    {"id":4,"employeeId":4,"employeeName":"陈静","attendanceDate":"2024-10-16","checkInTime":"09:00","checkOutTime":"17:30","status":40,"createTime":"2024-03-05 22:24:17","modifyTime":"2024-03-14 22:24:17"},
    {"id":5,"employeeId":3,"employeeName":"王强","attendanceDate":"2024-03-10","checkInTime":"08:45","checkOutTime":"18:10","status":20,"createTime":"2024-12-14 22:24:17","modifyTime":"2024-08-02 22:24:17"},
    {"id":6,"employeeId":1,"employeeName":"张伟","attendanceDate":"2024-05-14","checkInTime":"09:05","checkOutTime":"17:50","status":40,"createTime":"2024-11-07 22:24:17","modifyTime":"2024-03-12 22:24:17"},
    {"id":7,"employeeId":2,"employeeName":"李娜","attendanceDate":"2024-02-27","checkInTime":"08:55","checkOutTime":"18:00","status":20,"createTime":"2024-01-21 22:24:17","modifyTime":"2024-04-19 22:24:17"}
  ]
  constructor() {
  }

  save (attendanceForm: AttendanceForm): Observable<Result<void>> {
    console.log('attendanceApi save params', attendanceForm)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }

  find (attendanceQueryForm: AttendanceQueryForm): Observable<Result<Page<Attendance>>> {
    console.log('attendanceApi find params', attendanceQueryForm)
    return MockApi.queryPageSuccessfully(this.attendances).pipe(
      map((response: Response<Result<Page<Attendance>>>) => response.data)
    );
  }

  findById (id: number): Observable<Result<Attendance>> {
    console.log('attendanceApi findById id', id)
    let target = {}
    for (const a of this.attendances) {
      if (a.id === id) {
        target = a
      }
    }
    return MockApi.operateSuccessfullyWithData(target).pipe(
      map((response: Response<Result<Attendance>>) => response.data)
    );
  }

  del (id: number): Observable<Result<void>> {
    console.log('attendanceApi del id', id)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }
}
